<?php 
require_once 'phpQuery-onefile.php';
require_once 'TitleLinkAbstr.php';
require_once 'MySqlHelper.php';
require_once 'ShanghChangDetailMan.php';

$url = "http://www.shanghaidz.com/dazhe/gouwu/shangchangdazhe/";
if(isset($_POST['submit'])){
	$url = $_POST ['url'];
}

header('Content-Type: text/html; charset=UTF-8');
phpQuery::newDocumentFile($url);
$salesList = pq(".content-box-left ul:eq(0) li");
//echo ($salesList->html());
$infoList = array();
foreach($salesList as $sales){
	$title_link = pq($sales)->find('h1 a');
	$title_href= pq($title_link)->attr('href');
	$title = pq($title_link)->text();
	$abstract = pq($sales)->find('p');
	$titleLinkAbstr = new TitleLinkAbstr();
	$titleLinkAbstr->_abstract = $abstract->text();
	$titleLinkAbstr->_title = $title;
	$titleLinkAbstr->_link =  $title_href;
	$infoList[] = $titleLinkAbstr;
}
//print_r($infoList);
$mysqlHelper = MySqlHelper::getInstance();
foreach ($infoList as $titleLink){
	$date = date('Y-m-d H:i:s');
	$sql = "insert into act_link(url,title,abstract,createtime) values('%s','%s','%s','%s')";
	$sql_select = "select * from act_link where url='%s'";
	$sql_select = sprintf($sql_select,$titleLink->_link);
	$query = $mysqlHelper->query($sql_select);
	$rownum = $mysqlHelper->num_rows($query);
	if($rownum<1){
		$sql = sprintf($sql,$titleLink->_link,$titleLink->_title,$titleLink->_abstract,$date);
		$mysqlHelper->query($sql);
	}
}
	$sql = "select id,url from act_link where detail_id is null";
	$query = $mysqlHelper->query($sql);
	while($rs=$mysqlHelper->fetch_array($query)){
		//$rs = $mysqlHelper->fetch_row($query);
		//print_r($rs);
		$id = $rs['id'];
		$url = $rs['url'];
		$shangchang = new ShanghChangDetailMan();
		$detail_id = $shangchang->savePageDetail($url);
		$d_sql = "update act_link set detail_id='%s' where id='%s'";
		$d_sql = sprintf($d_sql,$detail_id,$id);
		echo "$id link,detail info $detail_id";
		$mysqlHelper->query($d_sql);
	}

?>


<form action="man.php" method="post">
	<p>
		new url: <input type="text" name="url" />
	</p>
	<input type="submit" name="submit" value="Submit" />
</form>